package com.sven.test02;

import com.alibaba.csp.sentinel.init.InitFunc;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;

import java.util.ArrayList;
import java.util.List;

//手动配置流控规则，还需要新建配置文件com.alibaba.csp.sentinel.init.InitFunc
public class FlowRuleInitFunc implements InitFunc {

    @Override
    public void init() throws Exception {
        //可以看出规则是个链表，那么意味着可以一个资源对应多个规则
        List<FlowRule> rules = new ArrayList<FlowRule>();
        FlowRule rule1 = new FlowRule();
        //规则设置资源名字，与SphU.entry保持一致
        rule1.setResource("doHello");
        //设置流控的规则的以QPS为准，还有以线程为准
        rule1.setGrade(RuleConstant.FLOW_GRADE_QPS);
        //设置现在qps为20
        rule1.setCount(2);
        //根据调用方进行流量控制，默认就是全部生效
        rule1.setLimitApp("default");
        rules.add(rule1);
        //添加进流控规则管理中
        FlowRuleManager.loadRules(rules);
    }
}
